﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.Mvc;

namespace MVC.Web.UI.Dialog
{
    public class WebDialogBuilder : IWebDialogBuilder
    {
        private  System.Web.Mvc.HtmlHelper _htmlHelper;
        private WebDialog _dialog = null;
       
        public WebDialogBuilder(System.Web.Mvc.HtmlHelper htmlHelper)
        {
            this._htmlHelper = htmlHelper;
            this._dialog = new WebDialog(this._htmlHelper);        
        }
       
        public IWebDialogBuilder Content(Action action) 
        {
            this._dialog.Content = action;   
            return this;
        }

        public IWebDialogBuilder Name(string name)
        {
            if (string.IsNullOrEmpty(name)||(string.IsNullOrWhiteSpace(name)))
                     throw new ArgumentNullException("name cannot be null ");

            this._dialog.Name = name;
            this._dialog.Attributes.Add("id", name);
            return this;
        }

        public IWebDialogBuilder AutoOpen(bool open)
       {
           this._dialog.AutoOpen = open;
           return this;
       }

        public IWebDialogBuilder CloseOnEscape(bool close)
        {
            this._dialog.CloseOnEscape = close;
            return this;
        }

        public IWebDialogBuilder CloseText(string closeText)
        {
            if (string.IsNullOrEmpty(closeText) || (string.IsNullOrWhiteSpace(closeText)))
                    throw new ArgumentException("You must supply a valid argument: closeText");

            this._dialog.CloseText = closeText;
            return this;
        }


        public IWebDialogBuilder DialogCssClass(string cssClass)
        {
            if (string.IsNullOrEmpty(cssClass) || (string.IsNullOrWhiteSpace(cssClass)))
                throw new  ArgumentException("You must supply a valid argument");

            this._dialog.Class = cssClass;
            return this;
        }


        public IWebDialogBuilder Draggable(bool draggable)
        {
            this._dialog.Draggable = draggable;
            return this;
        }


        public IWebDialogBuilder Height(string height)
        {
            if (string.IsNullOrEmpty(height) || (string.IsNullOrWhiteSpace(height)))
                throw new ArgumentException("You must supply a valid argument");

            this._dialog.Height = height;
            return this;
        }



        public IWebDialogBuilder MaxHeight(string maxHeight)
        {
            if (string.IsNullOrEmpty(maxHeight) || (string.IsNullOrWhiteSpace(maxHeight)))
                throw new ArgumentException("You must supply a valid argument");

            this._dialog.MaxHeight = maxHeight;
            return this;
        }


        public IWebDialogBuilder MaxWidth(string maxWidth)
        {
            if (string.IsNullOrEmpty(maxWidth) || (string.IsNullOrWhiteSpace(maxWidth)))
                throw new ArgumentException("You must supply a valid argument");

            this._dialog.MaxWidth = maxWidth;
            return this;
        }

        public IWebDialogBuilder MinHeight(int minHeight)
        {
            this._dialog.MinHeight = minHeight;
            return this;
        }


        public IWebDialogBuilder MinWidth(int minWidth)
        {
            this._dialog.MinWidth = minWidth;
            return this;
        }


        public IWebDialogBuilder Modal(bool modal)
        {
            this._dialog.Modal = modal;
            return this;
        }

        public IWebDialogBuilder Resizable(bool resizable)
        {
            this._dialog.Resizable = resizable;
            return this;
        }

        public IWebDialogBuilder Stack(bool stack)
        {
            this._dialog.Stack = stack;
            return this;
        }


        public IWebDialogBuilder Title(string title)
        {
            if (string.IsNullOrEmpty(title) || (string.IsNullOrWhiteSpace(title)))
                throw new ArgumentException("You must supply a valid argument");

            this._dialog.Title = title;
            return this;
        }

        public IWebDialogBuilder Width(int width)
        {
            this._dialog.Width = width;
            return this;
        }

        public IWebDialogBuilder ZIndex(int zindex)
        {
            this._dialog.ZIndex = zindex;
            return this;
        }

        private string ConfigureScript()
        {
            //Define the type of the dialog
            string typeDialog = null;

            switch (this._dialog.Type)
            {
                case DialogType.ModalMessage: typeDialog =  this._dialog.ModalMassage;
                    break;
                case DialogType.ModalConfirmation: typeDialog = this._dialog.ModalConfirmation;
                    break;
                default: typeDialog = "";
                    break;
            }

            // Create tag builder
            var scriptBuilder = new TagBuilder("script");
            scriptBuilder.MergeAttribute("type", "text/javascript");

            StringBuilder innerScriptBlock = new StringBuilder();
            innerScriptBlock.Append("\n\t$(function(){");
            innerScriptBlock.Append("\n\t  $('#" + this._dialog.Name + "').dialog({");
            
            string m = ((this._dialog.AutoOpen==false) ? ("\n\t\tcloseOnEscape: " + this._dialog.CloseOnEscape.ToString().ToLower()) + "," : "");              
            
            innerScriptBlock.Append(

            #region old code
                "\n\t\tautoOpen: " + this._dialog.AutoOpen.ToString().ToLower() + "," +
                ((!string.IsNullOrEmpty(this._dialog.CloseText)) ? ("\n\t\tcloseText: " + "'"+this._dialog.CloseText +"'" )  : "")              
                //"\n\t\tcloseText: " + this._dialog.CloseText + "," +
                //"\n\t\tdialogClass:" + this._dialog.Class
                ////"\n\t\tdraggable:" + this._draggable.ToString().ToLower() + "," +
                ////"\n\t\thide:" + this._hide + "," +
                ////"\n\t\theight:" + this._height + "," +
                ////"\n\t\tmaxHeight:" + this._maxHeight + "," +
                ////"\n\t\tmaxWidth:" + this._maxWidth + "," +
                ////"\n\t\tminHeight:" + this._minHeight + "," +
                ////"\n\t\tminWidth:" + this._minWidth + "," +
                ////"\n\t\tbuttons:{"+ typeDialog +"},"+ 

            ////"\n\t\tmodal:" + this._modal.ToString().ToLower() + "," +
                ////"\n\t\tposition:" + this._position + "," +
                ////"\n\t\tresizable:" + this._resizable.ToString().ToLower() + "," +
                ////"\n\t\tshow:" + this._show + "," +
                ////"\n\t\tstack:" + this._stack.ToString().ToLower() + "," +
                ////"\n\t\ttitle: '"+ this._title +"',"+
                ////"\n\t\twidth:" + this._width + "," +
                ////"\n\t\tzIndex:" + this._zIndex 
            #endregion

           );
            innerScriptBlock.Append("});");

            //Map all registered handlers to the client side events of the control    
            //foreach (KeyValuePair<DialogEvents , string> item in this._mapHandler)
            //{
            // innerScriptBlock
            // .Append("\n\t  $('#" + _dialogId + "').bind('"+((DialogEvents)item.Key).GetStringValue()+"',"+item.Value+")");
            //}

            innerScriptBlock.Append("\n\t});\n ");
            scriptBuilder.InnerHtml = innerScriptBlock.ToString();
            return "<!--  //-->" + "\n    " + scriptBuilder.ToString(TagRenderMode.Normal);
        }

        public void Render()
        {
            this._dialog.Scripts = this.ConfigureScript();
            this._dialog.RenderControl(this._htmlHelper.ViewContext);
        }
    }
}
